cmake_minimum_required(VERSION 3.8)

project(shell VERSION ${SIMODO_VERSION_MAJOR}.0.${SIMODO_VERSION_MINOR} LANGUAGES CXX)

if(${CROSS_WIN})
  enable_language("RC")
  set(ICON_RESOURCE ${CMAKE_CURRENT_SOURCE_DIR}/simodo_shell.rc)
endif()

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/../include/simodo/shell/Version.h.in
${CMAKE_CURRENT_SOURCE_DIR}/../include/simodo/shell/Version.h)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets PrintSupport)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets PrintSupport)

add_executable(${PROJECT_NAME}
    ${ICON_RESOURCE}
    src/DocumentCollector.cpp
    src/DocumentCollector.h
    src/DocumentWindow.cpp
    src/DocumentWindow.h
    src/GlobalMessages.cpp
    src/GlobalMessages.h
    src/GlobalMessageModel.cpp
    src/GlobalMessageModel.h
    src/GlobalMessageView.cpp
    src/GlobalMessageView.h
    src/Logger.cpp
    src/Logger.h
    src/LspBuilders.cpp
    src/LspBuilders.h
    src/LspManagement.cpp
    src/LspManagement.h
    src/main.cpp
    src/MainWindow.cpp
    src/MainWindow.h
    src/QRpc.cpp
    src/QRpc.h
    src/RunManagement.cpp
    src/RunManagement.h
    src/ShellAccess.cpp
    src/ShellAccess.h
    src/ShellMdiArea.cpp
    src/ShellMdiArea.h
    res/simodo_shell.qrc
    translations/simodo-shell_ru.ts 
)

target_link_libraries(${PROJECT_NAME} 
    Qt${QT_VERSION_MAJOR}::Widgets 
    Qt${QT_VERSION_MAJOR}::PrintSupport)

target_link_libraries(${PROJECT_NAME} SIMODO-module SIMODO-variable SIMODO-inout SIMODO-lsp-client SIMODO-tp)

target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/src/include)

set(PROJECT_OUTPUT_NAME "simodo-shell")
set_target_properties(
  ${PROJECT_NAME}
  PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
             MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
             MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
             MACOSX_BUNDLE TRUE
             WIN32_EXECUTABLE TRUE
             RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin
             RUNTIME_OUTPUT_NAME ${PROJECT_OUTPUT_NAME}
)

set(RELEASE_STRIP $<IF:$<CONFIG:Release>,${CMAKE_STRIP},>)
if(NOT (${RELEASE_STRIP}) STREQUAL "" AND (${CROSS_WIN}))
  set(PON_EXT .exe)
endif()
set(RELEASE_STRIP_ARGS
    $<IF:$<CONFIG:Release>,"${CMAKE_SOURCE_DIR}/bin/${PROJECT_OUTPUT_NAME}${PON_EXT}",>
)

add_custom_command(
  TARGET ${PROJECT_NAME}
  POST_BUILD
  COMMAND ${RELEASE_STRIP} ARGS ${RELEASE_STRIP_ARGS})

set(CPACK_GENERATOR "IFW")

set(CPACK_PACKAGE_NAME ${PROJECT_OUTPUT_NAME})
set(CPACK_PACKAGE_VERSION ${SIMODO_VERSION})
set(CPACK_PACKAGE_RELEASE ${SIMODO_VERSION_BUILD})
set(CPACK_PACKAGE_CONTACT "Michael Fetisov")
set(CPACK_PACKAGE_VENDOR "BMSTU")
set(CPACK_PACKAGE_FILE_NAME
    "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_PACKAGE_RELEASE}.${CMAKE_SYSTEM_PROCESSOR}"
)
set(CPACK_IFW_COMPONENT_INSTALL ON)

set(CPACK_IFW_PACKAGE_NAME "SIMODO edit")
set(CPACK_IFW_PACKAGE_START_MENU_DIRECTORY "Simodo")
set(CPACK_IFW_PACKAGE_TITLE "SIMODO edit Installer")
set(CPACK_IFW_PACKAGE_START_MENU_DIRECTORY "SIMODO")
set(CPACK_IFW_TARGET_DIRECTORY "@HomeDir@/Simodo/SimodoEdit")
set(CPACK_IFW_PACKAGE_ICON "${CMAKE_SOURCE_DIR}/installer/logo-01.ico")
set(CPACK_IFW_PACKAGE_WINDOW_ICON "${CMAKE_SOURCE_DIR}/installer/logo-01.png")
set(CPACK_IFW_PACKAGE_LOGO "${CMAKE_SOURCE_DIR}/installer/logo-01-128.png")
set(CPACK_IFW_PACKAGE_WIZARD_STYLE "Modern")
set(CPACK_IFW_PACKAGE_ALLOW_NON_ASCII_CHARACTERS ON)
set(CPACK_IFW_PRODUCT_URL "http://simodo.ru")
set(CPACK_IFW_ROOT "${CMAKE_SOURCE_DIR}/Qt/QtIFW")

include(CPackIFW)

cpack_ifw_configure_component(
  simodo-edit FORCED_INSTALLATION
  NAME simodo.edit
  DISPLAY_NAME "SIMODO edit"
  DESCRIPTION "Install SIMODO edit."
  SCRIPT ${CMAKE_SOURCE_DIR}/installer/installscript.qs
  LICENSES "MIT License" ${CMAKE_SOURCE_DIR}/installer/LICENSE.MIT
  DEFAULT true
  DEPENDS support)

cpack_ifw_configure_component(
  support FORCED_INSTALLATION VIRTUAL
  NAME support
  DEFAULT true)

if(${CROSS_WIN})
  install(
    PROGRAMS ${CMAKE_SOURCE_DIR}/bin/${PROJECT_OUTPUT_NAME}.exe
    DESTINATION bin
    COMPONENT simodo-edit)

  install(
    DIRECTORY ${CMAKE_SOURCE_DIR}/bin/
    DESTINATION bin
    COMPONENT simodo-edit
    PATTERN ${PROJECT_OUTPUT_NAME}.exe EXCLUDE)

else()
  install(
    CODE "
        execute_process(
            COMMAND rm -rf tmp
            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
            COMMAND_ERROR_IS_FATAL ANY
        )
        execute_process(
            COMMAND mkdir -p tmp/ifw-installer/bin
            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
            COMMAND_ERROR_IS_FATAL ANY
        )
        execute_process(
            COMMAND cp -r bin tmp/ifw-installer
            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
            COMMAND_ERROR_IS_FATAL ANY
        )
        execute_process(
            COMMAND ${CMAKE_CURRENT_LIST_DIR}/deploy.bash ${PROJECT_OUTPUT_NAME}
            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tmp/ifw-installer/bin
            COMMAND_ERROR_IS_FATAL ANY
        )
        "
    COMPONENT simodo-edit)

  install(
    PROGRAMS ${CMAKE_SOURCE_DIR}/tmp/ifw-installer/bin/${PROJECT_OUTPUT_NAME}
    DESTINATION bin
    COMPONENT simodo-edit)

  install(
    DIRECTORY ${CMAKE_SOURCE_DIR}/tmp/ifw-installer/
    DESTINATION .
    COMPONENT simodo-edit
    PATTERN bin/${PROJECT_OUTPUT_NAME}
      EXCLUDE
    PATTERN "bin/*"
      PERMISSIONS
        OWNER_EXECUTE OWNER_WRITE OWNER_READ
        GROUP_EXECUTE GROUP_READ
        WORLD_EXECUTE WORLD_READ
)
endif()

if(${CROSS_WIN})
  install(
    CODE "
        execute_process(
            COMMAND rm -rf tmp/data
            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
            COMMAND_ERROR_IS_FATAL ANY
        )
        execute_process(
            COMMAND mkdir -p tmp
            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
            COMMAND_ERROR_IS_FATAL ANY
        )
        execute_process(
            COMMAND cp -RL data tmp/data
            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
            COMMAND_ERROR_IS_FATAL ANY
        )
        "
    COMPONENT support)

  install(
    DIRECTORY "${CMAKE_SOURCE_DIR}/tmp/data"
    DESTINATION "."
    COMPONENT support
    PATTERN "*.cmake"
      EXCLUDE
    PATTERN "*.cmakein"
      EXCLUDE
  )

else()
  install(
    DIRECTORY "${CMAKE_SOURCE_DIR}/data"
    DESTINATION "."
    COMPONENT support
    PATTERN "*.cmake"
      EXCLUDE
    PATTERN "*.cmakein"
      EXCLUDE
    PATTERN "bin/*"
      PERMISSIONS
        OWNER_EXECUTE OWNER_WRITE OWNER_READ
        GROUP_EXECUTE GROUP_READ
        WORLD_EXECUTE WORLD_READ
  )
endif()

if(${CROSS_WIN})
  install(
    FILES "${CMAKE_SOURCE_DIR}/installer/icons/Simodo-simodoedit.ico"
    DESTINATION "icons"
    COMPONENT support)
else()
  install(
    DIRECTORY "${CMAKE_SOURCE_DIR}/installer/icons/"
    DESTINATION "icons"
    COMPONENT support
    PATTERN *.ico EXCLUDE)

endif()

install(
  DIRECTORY "${CMAKE_SOURCE_DIR}/test/examples"
  DESTINATION "."
  COMPONENT support)

include(CPack)
